package com.zeroup.auto.utils;

import com.ven.assists.stepper.Step;
import com.zeroup.auto.assists.next.NextStepBean;
import com.ven.assists.stepper.StepData;
import com.ven.assists.stepper.StepManager;

import kotlin.Unit;

public class AssistsUtils {

    public static Object getServiceData(Object data){
        if(data==null){
            return null;
        }
        Object object = ((StepData)data).getData();
        if(object instanceof NextStepBean){
            NextStepBean nextStepBean = (NextStepBean) object;
            return nextStepBean.getServiceData();
        }
        return null;
    }

    public static NextStepBean getNextBean(StepData data){
        if(data==null){
            return null;
        }
        Object object = data.getData();
        if(object instanceof NextStepBean){
            return (NextStepBean) object;
        }
        throw new RuntimeException("StepData getData type error。");
    }




    public static Step nextStep(Object data){
        if(data==null){
            return  Step.Companion.getNone();
        }
        StepData data1 = (StepData)data;
        Object object = data1.getData();
        if(object instanceof NextStepBean){
            NextStepBean nextStepBean = (NextStepBean) object;
            if(nextStepBean.getNext()==null){
                return  Step.Companion.getNone();
            }
            return start(nextStepBean.getNext(),2);
        }
        return  Step.Companion.getNone();
    }

    public static Step start(NextStepBean stepBean){
       return start(stepBean,1);
    }

    public static Step start(NextStepBean stepBean,int order){
        if(stepBean==null){
            return  Step.Companion.getNone();
        }
        StepData data = new StepData();
        data.setData(stepBean);
        System.out.println("步骤名称:===>"+stepBean.getName()+",参数:" + stepBean.getServiceData());
        try {
            Class  cl =   Class.forName(stepBean.gettClass());
            StepManager.INSTANCE.execute(cl, stepBean.getStep(),stepBean.getDelay()*1000,data,order==1);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return  Step.Companion.getNone();
    }


    public static void stop(){
        StepManager.INSTANCE.setStop(true);
    }
}
